/**
 * 
 */
$(document).ready(function() {
        // 指定websocket路径
        var websocket;
        if ('WebSocket' in window) {
			websocket = new WebSocket("ws://"+location.hostname +":"+ location.port + $("base").attr("href") +"/ws?uid=" + $(".uid").val());
		} else if ('MozWebSocket' in window) {
			websocket = new MozWebSocket("ws://"+location.hostname +":"+ location.port + $("base").attr("href") +"/ws" + $(".uid").val());
		} else {
			websocket = new SockJS("http://"+location.hostname +":"+ location.port + $("base").attr("href") +"/ws/sockjs" + $(".uid").val());
		}
        //var websocket = new WebSocket('ws://localhost:8080/Spring-websocket/ws');
        websocket.onmessage = function(event) {
       	 var data=JSON.parse(event.data);
       	 	if(data.from>0||data.from==-1){//用户或者群消息
            // 接收服务端的实时消息并添加到HTML页面中
            $("#log-container").append("<div class='bg-info'><label class='text-danger'>"+data.fromName+"&nbsp;"+data.date+"</label><div class='text-success'>"+data.text+"</div></div><br/>");
            // 滚动条滚动到最低部
            scrollToBottom();
            }else if(data.from==0){//上线消息
            	if(data.text != $(".username").text() )
            	{	
            		$("#users").append('<a onclick="talk(this)" class="list-group-item">'+data.text+'</a>');
            		alert(data.text+"上线了");
            	}
            }else if(data.from==-2){//下线消息
            	if(data.text!=$(".username").text())
            	{	
            		$("#users > a").remove(":contains('"+data.text+"')");
            		alert(data.text+"下线了");
            	}
            }
        };
        
        $.post("onlineusers",function(data){
    		for(var i=0;i<data.length;i++)
    			$("#users").append('<a onclick="talk(this)" class="list-group-item">'+data[i]+'</a>');
    	});
        
        $("#br-oadcast").click(function(){
        	$.post("broadcast",{"text":$("#msg").val()});
        });
        
        $("#send").click(function(){
        	var toname = $("body").data("to");
        	if(!toname){
        		alert("请选择一个用户！");
        		return false;
        	}
        	$.post("getuid",{"username": toname },function(d){
        		var v=$("#myinfo").val();
        		
				if(v==""){
					return;
				}else{
					var data={};
					data["from"] = $(".uid").val();
					data["fromName"] = $(".username").text();
					data["to"] = d.uid;
					data["text"] = v;
					console.log("send : ", data);
					websocket.send(JSON.stringify(data));
					$("#log-container").append("<div class='bg-success'><label class='text-info'>我&nbsp;"+new Date()+"</label><div class='text-info'>"+v+"</div></div><br/>");
					scrollToBottom();
					$("#myinfo").val("");
				}
        	});
        	
        });
        
    });
   
   function talk(a){
   	$("#talktitle").text("与"+a.innerHTML+"的聊天");
   	$("body").data("to",a.innerHTML);
   }
   function scrollToBottom(){
		var div = document.getElementById('log-container');
		div.scrollTop = div.scrollHeight;
	}